package com.example.hotel_lxy.ui.dialog;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.hotel_lxy.R;
import com.example.hotel_lxy.utils.EmptyUtils;
import com.ysxsoft.common_base.action.AnimAction;
import com.ysxsoft.common_base.adapter.BaseQuickAdapter;
import com.ysxsoft.common_base.adapter.BaseViewHolder;
import com.ysxsoft.common_base.base.BasePopupWindow;

import java.util.Arrays;
import java.util.List;

public class HotleChoiceDialog {
    public static final class Builder extends BasePopupWindow.Builder<HotleChoiceDialog.Builder>{
        private BaseQuickAdapter listAdapter;
        private OnListener onListener;
        public Builder(Context context) {
            super(context);
            RecyclerView recyclerView = new RecyclerView(context);
            recyclerView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            recyclerView.setBackgroundColor(getResources().getColor(R.color.colorWhite));
            setContentView(recyclerView);

            listAdapter=new BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_main_hotel){

                @Override
                protected void convert(BaseViewHolder helper, String item) {
                    if (EmptyUtils.isNotEmpty(item)){
                        helper.setText(R.id.tv_item_hall,item);
                    }
                }
            };
            recyclerView.setLayoutManager(new LinearLayoutManager(context));
            recyclerView.setAdapter(listAdapter);
            listAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    String itemData= (String) adapter.getData().get(position);
                    if (onListener!=null){
                        onListener.onSelected(getPopupWindow(),position,itemData);
                    }
                }
            });


        }

        @Override
        public Builder setGravity(int gravity) {
            switch (gravity) {
                // 如果这个是在中间显示的
                case Gravity.TOP:
                case Gravity.CENTER_VERTICAL:
                    // 重新设置动画
                    setAnimStyle(AnimAction.ANIM_SCALE);
                    break;
                default:
                    break;
            }
            return super.setGravity(gravity);
        }

        public Builder setList(List<String> data){
            if (EmptyUtils.isNotEmpty(data)&&data.size()>0&&EmptyUtils.isNotEmpty(listAdapter)){
                listAdapter.setNewData(data);
            }
            return this;
        }

        public Builder setOnListener(OnListener onListener){
            this.onListener=onListener;
            return this;
        }
        public Builder setList(String... data) {
            return setList(Arrays.asList(data));
        }


    }

    public interface OnListener<T> {

        /**
         * 选择条目时回调
         */
        void onSelected(BasePopupWindow popupWindow, int position, T t);
    }

}
